﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;

public partial class Data_XML_XMLDocument : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ReadXml();
        Response.Write(getConfigValue("MyTestParameter"));
        Utility.Utility.WriteLine(this, getConfigValue("MyTestParameter"));
    }

    private string getConfigValue(string ParameterName)
    {
        string conn = System.Configuration.ConfigurationManager.ConnectionStrings["MyCodeShopConnectionString"].ConnectionString;

        string ParameterValue = System.Configuration.ConfigurationManager.AppSettings[ParameterName].ToString();
        return ParameterValue;
    }

    private void ReadXml()
    {
        System.Xml.XmlDocument document = new XmlDocument();
        //document.Load(@"E:\Files\works\MyCodeShop\my-codeshop\MyWebSite\Data\XML\GhostStories.xml");      //  office
        //document.Load(@"D:\MyWorks\MyCodeShop\MyWebSite\Data\XML\GhostStories.xml");        //  home
        document.Load(@"E:\Files\works\MyCodeShop\my-codeshop\MyWebSite\Data\XML\GhostStories.xml");      //    Relative path
        System.Xml.XmlElement element = document.DocumentElement;       //      XmlElement 实例表示 XmlDocument 的根节点。

        //string text = FormatText(element as XmlNode, "", "");
        //text = getText(element as XmlNode, "");
        //lblText.Text = text;

        StringBuilder sb = new StringBuilder();
        //sb.Append(element.InnerText + "<br>");
        sb.Append(element.Name + "<br>");
        sb.Append(element.ChildNodes[0].ChildNodes[0].Name + "<br>");
        sb.Append(element.ChildNodes.Item(0).ParentNode.Name + "<br>");
        sb.Append(element.ChildNodes.Item(0).ChildNodes.Count + "<br>");
        lblText.Text = sb.ToString();
    }
    
    private string getText(XmlNode node, string text)
    {
        if (node is XmlText)
        {
            text += "<" + node.Name + ">";
        }

        return text;
    }

    private string FormatText(XmlNode node, string text, string indent)
    {
        if (node is XmlText)
        {
            text += node.Value;
            return text;
        }

        if (string.IsNullOrEmpty(indent))
            indent = "";
        else
        {
            text += "\r\n" + indent;
        }

        if (node is XmlComment)
        {
            text += node.OuterXml;
            return text;
        }

        text += "<" + node.Name;
        if (node.Attributes.Count > 0)
        {
            AddAttributes(node, ref text);
        }
        if (node.HasChildNodes)
        {
            text += ">";
            foreach (XmlNode child in node.ChildNodes)
            {
                text = FormatText(child, text, indent + "  ");
            }
            if (node.ChildNodes.Count == 1 &&
               (node.FirstChild is XmlText || node.FirstChild is XmlComment))
                text += "</" + node.Name + ">";
            else
                text += "\r\n" + indent + "</" + node.Name + ">";
        }
        else
            text += " />";
        return text;
    }

    private void AddAttributes(XmlNode node, ref string text)
    {
        foreach (XmlAttribute xa in node.Attributes)
        {
            text += " " + xa.Name + "='" + xa.Value + "'";
        }
    }

}